package com.sgl.gk.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.core.page.TableDataInfo;
import com.sgl.common.utils.StringUtils;
import com.sgl.gk.domain.GkProduct;
import com.sgl.gk.domain.GkStationConfigureTemplate;
import com.sgl.gk.mapper.GkProductMapper;
import com.sgl.gk.mapper.GkProductionLineMapper;
import com.sgl.gk.mapper.GkStationConfigureTemplateMapper;
import com.sgl.gk.service.GkProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class GkProductServiceImpl implements GkProductService {

    @Autowired
    GkProductMapper gkProductMapper;

    @Autowired
    GkProductionLineMapper gkProductionLineMapper;

    @Autowired
    GkStationConfigureTemplateMapper gkStationConfigureTemplateMapper;

    @Override
    public List<GkProduct> list(GkProduct gkProduct) {
        List<GkProduct> list = gkProductMapper.list(gkProduct);
        if (list==null||list.size()==0){
            return list;
        }
        List<String> trmplateNames = list.stream().map(GkProduct::getTemplateName).distinct().collect(Collectors.toList());
        List<GkStationConfigureTemplate> gkStationConfigureTemplates = gkStationConfigureTemplateMapper.selectInfoByNames(trmplateNames);
        Map<String, List<GkStationConfigureTemplate>> collect = gkStationConfigureTemplates.parallelStream().collect(Collectors.groupingBy(GkStationConfigureTemplate::getName));
        for (GkProduct product : list) {
            if (StringUtils.isEmpty(product.getTemplateName())) {
                product.setTemplateNum(0);
            }else {
                product.setTemplateNum(collect.get(product.getTemplateName())==null?0:collect.get(product.getTemplateName()).size());
            }
        }
        return list;
    }

    @Override
    public AjaxResult add(GkProduct gkProduct) {
        String name = gkProduct.getName();
        List<GkProduct> gkProducts = gkProductMapper.selectByNames(Collections.singletonList(name));
        if (CollUtil.isNotEmpty(gkProducts)){
            return AjaxResult.error("产品名称不可以重复");
        }
        gkProduct.setName(StringUtils.trim(name));
        gkProduct.setCode(StringUtils.trim(gkProduct.getCode()));
        gkProduct.setCreateAt(new Date());
        gkProduct.setLastModifyAt(new Date());
        int row = gkProductMapper.add(gkProduct);
        if (row>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult edit(GkProduct gkProduct) {
        int row = gkProductMapper.edit(gkProduct);
        String name = gkProduct.getName();
        Long id = gkProduct.getId();
        List<GkProduct> gkProducts = gkProductMapper.selectByNameExclude(name,id);
        if (CollUtil.isNotEmpty(gkProducts)){
            return AjaxResult.error("产品名称不可以重复");
        }
        if (row>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult removeByIds(Long[] ids) {
        int row = gkProductMapper.removeByIds(ids);
        if (row>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
